﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using DiagramCreator.Nodes;
using System.Windows.Input;
using DiagramCreator.Arrows;
using System.Windows.Media;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace DiagramCreator.Diagram
{
    public partial class DiagramSheet : Canvas, IDisposable
    {
        /// <summary>
        /// Sets new size for diagram sheet
        /// </summary>
        /// <param name="s"></param>
        void SetSheetSize(Size s)
        {
            this.SetValue(Canvas.HeightProperty, s.Height);
            this.SetValue(Canvas.WidthProperty, s.Width);
        }

        /// <summary>
        /// Measures minimal size, covering each child control
        /// </summary>
        /// <returns></returns>
        Size MeasureActualChildControlsAreaSize()
        {
            double w = ((ScrollViewer)this.Parent).ActualWidth;
            double h = ((ScrollViewer)this.Parent).ActualHeight;
            double tmp;
            bool bigger = false;
            foreach (FrameworkElement x in Children)
            {
                tmp = x.Width + (double)x.GetValue(Canvas.LeftProperty);
                if (w < tmp) { w = tmp; bigger = true; }
                tmp = x.Height + (double)x.GetValue(Canvas.TopProperty);
                if (h < tmp) { h = tmp; bigger = true; }
            }
            if (!bigger)
            {
                w = double.NaN;
                h = double.NaN;
            }
            return new Size(w, h);
        }

        /// <summary>
        /// sets size to minimal size needed to show every child control
        /// </summary>
        public void AdjustSizeToChildrenControls()
        {
            SetSheetSize(MeasureActualChildControlsAreaSize());
        }
    }
}